package com.wlcorp.forcazamis.business.pi.impl;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import com.wlcorp.forcazamis.business.pi.IPidetailService;
import com.wlcorp.forcazamis.dao.baseinfo.ICustomerDAO;
import com.wlcorp.forcazamis.dao.baseinfo.ISampleSourceDAO;
import com.wlcorp.forcazamis.dao.pi.IPidetailDAO;
import com.wlcorp.forcazamis.dao.pi.IPifittingDAO;
import com.wlcorp.forcazamis.dao.pi.IPiproductDetailDAO;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.domain.pi.Pidetail;
import com.wlcorp.forcazamis.domain.pi.Pifitting;
import com.wlcorp.forcazamis.domain.pi.PiproductDetail;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;

public class PidetailServiceImpl extends BaseService implements
		IPidetailService {
	private IPidetailDAO pidetailDAO;
	private IPiproductDetailDAO piproductDetailDAO;
	private IPifittingDAO pifittingDAO;
	private ICustomerDAO customerDAO;
	private ISampleSourceDAO sampleSourceDAO;
	public void setPidetailDAO(IPidetailDAO pidetailDAO) {
		this.pidetailDAO = pidetailDAO;
	}

	public void setPiproductDetailDAO(IPiproductDetailDAO piproductDetailDAO) {
		this.piproductDetailDAO = piproductDetailDAO;
	}

	public void setPifittingDAO(IPifittingDAO pifittingDAO) {
		this.pifittingDAO = pifittingDAO;
	}

	public void setCustomerDAO(ICustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public void setSampleSourceDAO(ISampleSourceDAO sampleSourceDAO) {
		this.sampleSourceDAO = sampleSourceDAO;
	}

	public List<Pidetail> getPidetailList(Pidetail pidetail) throws ServiceException {
		return pidetailDAO.getPidetailList(pidetail);
	}

	public Long getPidetailCount(Pidetail pidetail) throws ServiceException {
		return pidetailDAO.getPidetailCount(pidetail);
	}

	public Pidetail getPidetailById(Long pidetailId) throws ServiceException {
		if(null == pidetailId){
			return null;
		}
		return pidetailDAO.getPidetailById(pidetailId);
	}

	public void deletePidetail(Pidetail pidetail) throws ServiceException {
		pidetailDAO.deletePidetail(pidetail);
	}

	public void savePidetail(Pidetail pidetail) throws ServiceException {
		Customer customer = null;
		if(null != pidetail.getCustomerId()){
			customer = customerDAO.getCustomerById(pidetail.getCustomerId());
			pidetail.setCustomer(customer);
		}
		if(null != pidetail.getNewPiSource()){
			pidetail.setSource(sampleSourceDAO.getSampleSourceById(pidetail.getNewPiSource()));
		}

		if(null == pidetail.getPiid()){
			pidetail.setState(AppConstant.BILL_STATE_SUBMITED);
//			产生PI编号
			pidetail.setPiCode(createPiCode(customer));
			pidetail.setTotalSum(0.0);
			pidetail.setTotalQuantity(0l);
			pidetail.setNewPiSum(0.0);
			pidetail.setNewPiQuantity(0l);
			pidetail.setRePiSum(0.0);
			pidetail.setRePiQuantity(0l);
			pidetail.setOtherSum(0.0);
		}
		pidetailDAO.savePidetail(pidetail);
	}
	
	public void deletePiproductDetail(PiproductDetail piproductDetail) throws ServiceException {
		
		piproductDetailDAO.deletePiproductDetail(piproductDetail);
	}

	public void savePiproductDetailList(List<PiproductDetail> piproductList,Long piid) throws ServiceException {
		//删除原来与合同关联的产品数据
		Pidetail pidetail = pidetailDAO.getPidetailById(piid);
		PiproductDetail tmpPiproductDetail = new PiproductDetail();
		tmpPiproductDetail.setPiid(piid);
		List<PiproductDetail> tmpProductList = piproductDetailDAO.getPiproductDetailList(tmpPiproductDetail);
		double totalSum = 0.0;
		long totalQuantity = 0;
		double newPiSum = 0.0;
		long newPiQuantity = 0;
		double rePiSum = 0.0;
		long rePiQuantity = 0;
		for(Iterator<PiproductDetail> iter = tmpProductList.iterator();iter.hasNext();){
			PiproductDetail piproductDetail = (PiproductDetail)iter.next();
			piproductDetailDAO.deletePiproductDetail(piproductDetail);
		}
		//保存新的产品数据并计算总金额，翻单金额，新单金额
		for(Iterator<PiproductDetail> iter = piproductList.iterator();iter.hasNext();){
			PiproductDetail piproductDetail = (PiproductDetail)iter.next();
			totalSum = totalSum + piproductDetail.getTotalSum();
			totalQuantity = totalQuantity + piproductDetail.getProdCount();
			piproductDetail.setPidetail(pidetail); 
			if(AppConstant.PI_PRODUCT_IS_NEW == piproductDetail.getIsNew()){
				newPiSum = newPiSum + piproductDetail.getTotalSum();
				newPiQuantity = newPiQuantity + piproductDetail.getProdCount();
			}else{
				rePiSum = rePiSum + piproductDetail.getTotalSum();
				rePiQuantity = rePiQuantity + piproductDetail.getProdCount();
			}
			piproductDetailDAO.savePiproductDetail(piproductDetail);
		}
		totalSum = (totalSum + pidetail.getOtherSum()) * pidetail.getTotalDiscount();
		
		//更新PI的金额信息
		pidetail.setTotalSum(totalSum);
		pidetail.setTotalQuantity(totalQuantity);
		pidetail.setNewPiSum(newPiSum);
		pidetail.setNewPiQuantity(newPiQuantity);
		pidetail.setRePiSum(rePiSum);
		pidetail.setRePiQuantity(rePiQuantity);
		pidetailDAO.savePidetail(pidetail);
	}

	public PiproductDetail getPiproductDetailById(Long productId) throws ServiceException {
		if(null == productId){
			return null;
		}
		return piproductDetailDAO.getPiproductDetailById(productId);
	}

	public List<Pifitting> getPifittingList(Pifitting pifitting) throws ServiceException {
		return pifittingDAO.getPifittingList(pifitting);
	}

	public void deletePifitting(Pifitting pifitting) throws ServiceException {
		pifittingDAO.deletePifitting(pifitting);
		//重新计算价格
		Pidetail pidetail = pifitting.getPidetail();
		pidetail.setTotalSum(pidetail.getTotalSum() - pifitting.getTotalSum());
		pidetail.setOtherSum(pidetail.getOtherSum() - pifitting.getTotalSum());
		pidetailDAO.savePidetail(pidetail);
	}

	public void savePifitting(Pifitting pifitting) throws ServiceException {
		Pidetail pidetail = pidetailDAO.getPidetailById(pifitting.getPiid());
		pifitting.setPidetail(pidetail);
		pifittingDAO.savePifitting(pifitting);
//		重新计算PI其他金额与总金额
		double otherSum = 0.0;
		double totalSum = 0.0;
		Pifitting tmpPifitting = new Pifitting();
		tmpPifitting.setPiid(pifitting.getPiid());
		List<Pifitting> pifittingList = pifittingDAO.getPifittingList(tmpPifitting);
		for(Iterator<Pifitting> iter = pifittingList.iterator();iter.hasNext();){
			tmpPifitting = (Pifitting)iter.next();
			otherSum = otherSum + tmpPifitting.getTotalSum();
		}
		totalSum = pidetail.getTotalSum() - pidetail.getOtherSum();
		totalSum = totalSum + otherSum;
		pidetail.setOtherSum(otherSum);
		pidetail.setTotalSum(totalSum);
		pidetailDAO.savePidetail(pidetail);
	}

	public Pifitting getPifittingById(Long fittingId) throws ServiceException {
		return pifittingDAO.getPifittingById(fittingId);
	}

	public List<PiproductDetail> getPiproductDetailList(PiproductDetail piproductDetail) throws ServiceException {
		return piproductDetailDAO.getPiproductDetailList(piproductDetail);
	}
	
	public List<PiproductDetail> getPiproductDetailListByCode(PiproductDetail piproductDetail) throws ServiceException {
		return piproductDetailDAO.getPiproductDetailListByCode(piproductDetail);
	}
	
	private String createPiCode(Customer customer){
		String piCode = pidetailDAO.getLatestPiCodeByCustomer(String.valueOf(customer.getCustomerId()));
		int num = 0;
		if(null == piCode){
			num = 1;
		}else{
			String[] piCodeSp = piCode.split("-");
			num = Integer.parseInt(piCodeSp[2]) + 1;
		}
		int year = Calendar.getInstance().get(Calendar.YEAR);
		String yearStr = String.valueOf(year).substring(2,4);
		String numStr = num / 10 == 0?"0" + num:String.valueOf(num);
		String newPiCode = "F" + yearStr + "A-" + customer.getCustomerName()
			+ "-" + numStr;
		return newPiCode;
	}

	public void drawbackPI(Pidetail pidetail) throws ServiceException {
		pidetail.setState(AppConstant.BILL_STATE_SUBMITED);
//		Confirm confirm = pidetail.getConfirm();
//		Department department = confirm.getSubmiter().getDepartment();
//		confirm.setNextConfirmer(department.getManager());
		pidetailDAO.savePidetail(pidetail);
		
	}

	public List<Pifitting> getPifittingListByPidetailId(Long piid) throws ServiceException {
		return pifittingDAO.getPifittingListByPidetailId(piid);
	}

	public List<PiproductDetail> getPiproductDetailListByPidetailId(Long piid)
			throws ServiceException {
		return piproductDetailDAO.getPiproductDetailListByPidetailId(piid);
	}

	public void updatePidetail(Pidetail pidetail) throws ServiceException {
		piproductDetailDAO.updatePidetail(pidetail);
	}

}
